package canglang.monitor.core.codelookup.service.impl;

import java.util.ArrayList;
import java.util.List;

import canglang.monitor.core.codelookup.config.CodeLookupBean;
import canglang.monitor.core.codelookup.config.CodeLookupConfig;
import canglang.monitor.core.codelookup.dao.CodeLookupDAO;
import canglang.monitor.core.codelookup.exception.CodeLookupException;
import canglang.monitor.core.codelookup.model.CodeModel;
import canglang.monitor.core.codelookup.service.CodeLookupMgr;


@SuppressWarnings("unchecked")
public class CodeLookupMgrImpl implements CodeLookupMgr {

	private static CodeLookupDAO dao = null;

	private CodeLookupDAO getDAOInstance() {
		if (dao != null)
			return dao;
		dao = new CodeLookupDAO();
		return dao;
	}

	public CodeModel getCode(String codeType, String codeId)
			throws CodeLookupException {

		CodeModel codeTO;
		try {
			CodeLookupBean lookupBean = getLookupBean(codeType);
			// 1. Get the active code and set CodeTO.active = true.
			// 2. If no active code is found
			// then get the non-active one and set CodeTO.active = false.
			CodeLookupDAO dao = getDAOInstance();
			codeTO = dao.getCode(lookupBean, codeId);
		} catch (Throwable ex) {
			ex.printStackTrace();
			throw new CodeLookupException(codeType, codeId, ex);
		}
		return codeTO;

	}

	public List getCodeList(String codeType) throws CodeLookupException {
		return getCodeList(codeType, true);
	}

	public List getCodeList(String codeType, boolean activeOnly)
			throws CodeLookupException {
		List list = null;
		try {
			CodeLookupBean lookupBean = getLookupBean(codeType);
			CodeLookupDAO dao = getDAOInstance();
			if (activeOnly) {
				list = dao.getCodeList(lookupBean, null, null, new ArrayList());
			} else {
				list = dao.getCodeList(lookupBean);
			}
			// Store code list into the cache.
			if (list == null) {
				list = new ArrayList();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}
	
	public List getCodeList(String codeType, String filterName, List filterParamList) throws CodeLookupException {

        List codeList;
        try {
            CodeLookupBean lookupBean = getLookupBean(codeType);
            CodeLookupDAO dao =getDAOInstance();
            if (filterParamList == null) {
                filterParamList = new ArrayList();
            }
            codeList = dao.getCodeList(lookupBean, null, filterName, filterParamList);
        } catch(Throwable ex) {
            throw new CodeLookupException(codeType, ex);
        }
        return codeList;

    }	

	private CodeLookupBean getLookupBean(String codeType)
			throws CodeLookupException {

		CodeLookupConfig config = CodeLookupConfig.getInstance();
		CodeLookupBean lookupBean = config.getCodeLookup(codeType);
		if (lookupBean == null) {
			String errorMsg = "Code type[" + codeType
					+ "] is not configured in config file.";

			throw new CodeLookupException(codeType, errorMsg);
		}
		return lookupBean;
	}

    public List getCodeListCodes(String codeType) throws CodeLookupException{
        List l=new ArrayList();
        l.add(codeType);
       List list=this.getCodeList("CODE_TYPE","CODE_FILTER",l);
       List rList=new ArrayList();
       if(list!=null&&list.size()>0){
            for(int i=0;i<list.size();i++){
                CodeModel code=(CodeModel)list.get(i);
                rList.add(code.getCodeId());
            }
       }
        return rList;
    }

    public String getCodeDesc(String codeType,String codeId) throws CodeLookupException{
        List l=new ArrayList();
        l.add(codeType);
        l.add(codeId);
        List codeList = this.getCodeList("CODE_TYPE_DESC", "CODE_FILTER", l);
        if(codeList!=null&& codeList.size()>0)return ((CodeModel)codeList.get(0)).getCodeDesc();
        return "";
    }

}
